package a66weding.com.jiehuntong.activity.toolsbudget;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.HashMap;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.activity.SearchCityActivity;
import a66weding.com.jiehuntong.beans.BudgetBean;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.common.utils.CUtils;
import a66weding.com.jiehuntong.common.utils.CacheUtils;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;
import a66weding.com.jiehuntong.http.UrlDealUtil;

public class ToolsBudgetAddbudgetActivity extends BaseBudgetActivity implements View.OnClickListener, OnNetResultListener {
    private TextView mTitleTv;
    private TextView mBackTv;
    private TextView mHeadRightfuncTv;
    private TextView mPlaceTv;
    private LinearLayout mSelectPlaceLay;
    private LinearLayout mBackLay;
    private EditText mMoneyCountEt;
    private EditText mSeatCountEt;
    private Button mCommitBtn;
    private String returnwbitemurl;
    private String seatCount;
    private String moneyCount;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tools_budget_addbudget);
        initview();
    }
    private void initview() {
        mTitleTv = (TextView) findViewById(R.id.tv_header_title);
        mBackTv = (TextView) findViewById(R.id.tv_header_backcontent);
        mBackLay = (LinearLayout) findViewById(R.id.llay_header_back);
        mSelectPlaceLay = (LinearLayout) findViewById(R.id.lay_header_place);
        mHeadRightfuncTv = (TextView) findViewById(R.id.tv_header_function);
        mMoneyCountEt = (EditText) findViewById(R.id.et_tools_budget_moneycount);
        mSeatCountEt = (EditText) findViewById(R.id.et_tools_budget_seatcount);
        mCommitBtn = (Button) findViewById(R.id.btn_tools_budget_editcommit);
        mPlaceTv = (TextView) findViewById(R.id.tv_tools_wedregistry_place);
        mTitleTv.setText("结婚预算");
        mSelectPlaceLay.setVisibility(View.GONE);
        mHeadRightfuncTv.setVisibility(View.GONE);
        mCommitBtn.setOnClickListener(this);
        mSelectPlaceLay.setOnClickListener(this);
        mBackLay.setOnClickListener(this);
        mTitleTv.setOnClickListener(this);

    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
                case R.id.btn_tools_budget_editcommit:
                    boolean isRight=checkInput();
                    if(isRight) {
                        moneyCount = mMoneyCountEt.getText().toString().trim();
                        int mCount = Integer.valueOf(moneyCount);
                        super.setBudgetCount(mCount);
                        seatCount = mSeatCountEt.getText().toString().trim();
                        int mSeatCount = Integer.valueOf(seatCount);
                        super.setTableCount(mSeatCount);
                        returnwbitemurl = UrlDealUtil.getRealUrl(RequestCode.RETURN_WBITEMSYS);
                        HashMap<String,String> requestParams=new HashMap<String,String>();
                        requestParams.put("BudgetMoney",""+mCount);
                        requestParams.put("BudgetTables",""+mSeatCount);
                        HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.POSTEQUEST, returnwbitemurl,
                                this, BudgetBean.class, RequestCode.RETURN_WBITEMSYS, requestParams);
                        HttpHandleUtil.executeCommonPost(ToolsBudgetAddbudgetActivity.this,httpRequestBean);
                        showCommitPopupWindow(this,view);
                    }

                    break;
                case R.id.lay_header_place:
                    Intent intent = new Intent(this, SearchCityActivity.class);
                    intent.putExtra("city",mPlaceTv.getText().toString().trim());
                    startActivityForResult(intent,GETPLACERESULTCODE);
                break;
                case R.id.llay_header_back:
                   finish();
                break;
                case R.id.tv_header_title:
                    break;
        }
    }

    private boolean checkInput() {
        String moneyCount = mMoneyCountEt.getText().toString().trim();
        try {
            if (moneyCount.length() > 10) {
                CUtils.showToast("输入的金额过大,请重新输入");
                return false;
            }
            if (TextUtils.isEmpty(moneyCount)) {
                CUtils.showToast("输入的金额不能为空");
                return false;
            }
            int mCount = Integer.valueOf(moneyCount);
            if (mCount < 20000) {
                CUtils.showToast("输入的金额不能小于2万");
                return false;
            }
            String seatCount = mSeatCountEt.getText().toString().trim();
            if (seatCount.length() > 8) {
                CUtils.showToast("输入的婚宴桌数过大,请重新输入");
                return false;
            }
            if (TextUtils.isEmpty(seatCount)) {
                CUtils.showToast("输入的婚宴桌数不能为空");
                return false;
            }
            int mSeatCount = Integer.valueOf(seatCount);
            if (mCount < 1) {
                CUtils.showToast("输入的婚宴桌数不能小于1桌");
                return false;
            }
            return true;
        }catch (Exception e){
            return false;
        }

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==GETPLACERESULTCODE) {
            if(data!=null) {
                String city = data.getStringExtra("city");
                mPlaceTv.setText(city);
            }
        }
    }



    @Override
    public void onSucess(SuccessBean sucessbean) {
        if(mCommitpop!=null) {
            mCommitpop.dismiss();
        }
        Object data = sucessbean.getData();

        if(data instanceof BudgetBean) {
            BudgetBean mBudgitBean= (BudgetBean) data;
            switch (mBudgitBean.getResCode()) {
                case "0000":
                    Toast.makeText(this,"预算成功！",Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(this, ToolsBudgetActivity.class);
                    //由于后台返回来的数据没有总的预算价,以及桌数,所以这里手动添加上
                    mBudgitBean.setMoneyCount(Integer.valueOf(moneyCount));
                    mBudgitBean.setSeatCount(Integer.valueOf(seatCount));
                    mBudgitBean.setBudgetTables(Integer.valueOf(seatCount));
                    //暂时预算保存本地,稍后另一个界面取出
                    CacheUtils.saveObjectToTextFile(returnwbitemurl,mBudgitBean);
                    intent.putExtra("isFromTools",true);
                    startActivity(intent);
                    finish();
                    break;
                default:
                    CUtils.showToast("R.string.dataerror");
                    break;

            }
        }
    }

    @Override
    public void onError(ErrorBean errorBean) {
        if(mCommitpop!=null) {
            mCommitpop.dismiss();
        }
        CUtils.showToast("R.string.dataerror");
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View view = getCurrentFocus();
            if (isHideInput(view, ev)) {
                HideSoftInput(view.getWindowToken());
            }
        }
        return super.dispatchTouchEvent(ev);
    }
    // 判定是否需要隐藏
    private boolean isHideInput(View v, MotionEvent ev) {
        if (v != null && (v instanceof EditText)) {
            int[] l = { 0, 0 };
            v.getLocationInWindow(l);
            int left = l[0], top = l[1], bottom = top + v.getHeight(), right = left
                    + v.getWidth();
            if (ev.getX() > left && ev.getX() < right && ev.getY() > top
                    && ev.getY() < bottom) {
                return false;
            } else {
                return true;
            }
        }
        return false;
    }
    // 隐藏软键盘
    private void HideSoftInput(IBinder token) {
        if (token != null) {
            InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            manager.hideSoftInputFromWindow(token,
                    InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }


}
